package top.tangyh.lamp.care.common;

import cn.hutool.core.date.DateUtil;
import top.tangyh.lamp.common.utils.YDateUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

public class CareCommonUtil {
    /**
     * 从一个List里删除包含另一个List的数据
     *
     * @param listA 需要被去除内容的list
     * @param listB 去除的内容
     * @return
     */
    public static List<String> listRemove(List<String> listA, List<String> listB) {
        HashSet hs1 = new HashSet(listA);
        HashSet hs2 = new HashSet(listB);
        hs1.removeAll(hs2);
        List<String> listC = new ArrayList<String>();
        listC.addAll(hs1);
        return listC;
    }

    /**
     * 体温单时间区间
     * //            2区间展示的是：00:00:00-03:59:59
     * //            6区间展示的是：04:00:00-7:59:59
     * //            10区间展示的是：08:00:00-11:59:59
     * //            14区间展示的是：12:00:00-15:59:59
     * //            18区间展示的是：16:00:00-19:59:59
     * //            22区间展示的是：20:00:00-23:59:59
     * 得到该时间所在的体温单时间区间
     *
     * @param time
     * @return
     */
    public static String getDayHourDiff(Date time) {
        Date date2Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 00:00:00");
        Date date2End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 03:59:59");

        Date date6Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 04:00:00");
        Date date6End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 07:59:59");

        Date date10Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 08:00:00");
        Date date10End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 11:59:59");

        Date date14Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 12:00:00");
        Date date14End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 15:59:59");

        Date date18Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 16:00:00");
        Date date18End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 19:59:59");

        Date date22Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 20:00:00");
        Date date22End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 23:59:59");

        Boolean is2 = YDateUtils.belongCalendarAndEqual(time, date2Start, date2End);
        Boolean is6 = YDateUtils.belongCalendarAndEqual(time, date6Start, date6End);
        Boolean is10 = YDateUtils.belongCalendarAndEqual(time, date10Start, date10End);
        Boolean is14 = YDateUtils.belongCalendarAndEqual(time, date14Start, date14End);
        Boolean is18 = YDateUtils.belongCalendarAndEqual(time, date18Start, date18End);
        Boolean is22 = YDateUtils.belongCalendarAndEqual(time, date22Start, date22End);
        if (is2) {
            return DateUtil.formatDate(time) + "-2";
        } else if (is6) {
            return DateUtil.formatDate(time) + "-6";
        } else if (is10) {
            return DateUtil.formatDate(time) + "-10";
        } else if (is14) {
            return DateUtil.formatDate(time) + "-14";
        } else if (is18) {
            return DateUtil.formatDate(time) + "-18";
        } else if (is22) {
            return DateUtil.formatDate(time) + "-22";
        } else {
            return "";
        }

    }


    /**
     * 根据当前时间得到前一个区间
     *
     * @param time
     * @return
     */
    public static String getprevioustDayHourDiff(Date time) {
        //得到当前体温单区间
        String nowDayHourDiff = getDayHourDiff(time);

        String previoustDayHourDiff = "";
        List<String> list = getAllDayHourDiffList(time);
//        List<String> list = new ArrayList<>();
//        list.add("2022-10-18-2");
//        list.add("2022-10-18-6");
//        list.add("2022-10-18-10");
//        list.add("2022-10-18-14");
//        list.add("2022-10-18-18");
//        list.add("2022-10-18-22");

//        String dayHourDiff="2022-10-18-10";

        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(nowDayHourDiff)) {
                index = i;
                break;
            }
        }
        if (index != 0) {
            previoustDayHourDiff = list.get(index - 1);
//            System.out.println(list.get(index-1));
        } else {
            previoustDayHourDiff = list.get(index);
            //System.out.println(list.get(index));
        }
        return previoustDayHourDiff;
    }

    /**
     * 得到当前时间的所有区间
     *
     * @param time
     * @return
     */
    public static List<String> getAllDayHourDiffList(Date time) {
        List<String> listDayHourDiff = new ArrayList<>();
//        Date date2Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 00:00:00");
//        Date date2End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 03:59:59");
//
//        Date date6Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 04:00:00");
//        Date date6End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 07:59:59");
//
//        Date date10Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 08:00:00");
//        Date date10End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 11:59:59");
//
//        Date date14Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 12:00:00");
//        Date date14End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 15:59:59");
//
//        Date date18Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 16:00:00");
//        Date date18End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 19:59:59");
//
//        Date date22Start = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 20:00:00");
//        Date date22End = DateUtil.parseDateTime(DateUtil.formatDate(time) + " 23:59:59");

//        Boolean is2 = YDateUtils.belongCalendarAndEqual(time, date2Start, date2End);
//        Boolean is6 = YDateUtils.belongCalendarAndEqual(time, date6Start, date6End);
//        Boolean is10 = YDateUtils.belongCalendarAndEqual(time, date10Start, date10End);
//        Boolean is14 = YDateUtils.belongCalendarAndEqual(time, date14Start, date14End);
//        Boolean is18 = YDateUtils.belongCalendarAndEqual(time, date18Start, date18End);
//        Boolean is22 = YDateUtils.belongCalendarAndEqual(time, date22Start, date22End);
        listDayHourDiff.add(DateUtil.formatDate(time) + "-2");
        listDayHourDiff.add(DateUtil.formatDate(time) + "-6");
        listDayHourDiff.add(DateUtil.formatDate(time) + "-10");
        listDayHourDiff.add(DateUtil.formatDate(time) + "-14");
        listDayHourDiff.add(DateUtil.formatDate(time) + "-18");
        listDayHourDiff.add(DateUtil.formatDate(time) + "-22");

//        if(is2){
//            return DateUtil.formatDate(time)+"-2";
//        }else if(is6){
//            return DateUtil.formatDate(time)+"-6";
//        }else if(is10){
//            return DateUtil.formatDate(time)+"-10";
//        }else if(is14){
//            return DateUtil.formatDate(time)+"-14";
//        }else if(is18){
//            return DateUtil.formatDate(time)+"-18";
//        }else if(is22){
//            return DateUtil.formatDate(time)+"-22";
//        }else {
//            return "";
//        }
        return listDayHourDiff;
    }

    /**
     * 获取当前时间前一小时的时间
     *
     * @param date
     * @return java.util.Date
     */
    public static Date beforeOneHourToNowDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        return calendar.getTime();
    }

    /**
     * 多少分钟之前
     *
     * @param date
     * @return
     */
    public static Date beforeMinutesToNowDate(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -minute);
        return calendar.getTime();
    }


    /**
     * 比较两个时间相差小时
     */
    public static double calculatetimeGapHour(Date date1, Date date2) {
        double hour = 0;
        double millisecond = date2.getTime() - date1.getTime();
        hour = millisecond / (60 * 60 * 1000);
        return hour;
    }

    public static int calculatetimeGapMinute(Date date1, Date date2) {
        Long minute = 0l;
        Long millisecond = date2.getTime() - date1.getTime();
        minute = millisecond / (60 * 1000);
        return minute.intValue();
    }
//    public static void main(String[] args) throws ParseException {
////        Date date=new Date();
////        Calendar calendar = Calendar.getInstance();
////        /* HOUR_OF_DAY 指示一天中的小时 */
////        calendar.setTime(date);
////        calendar.add(Calendar.MINUTE, -30);
////        System.out.println( calendar.getTime());
////        Date date1=DateUtil.parseDateTime("2022-10-19 12:44:00");
////            Date date2=DateUtil.parseDateTime("2022-10-19 13:00:00");
////        System.out.println(calculatetimeGapMinute(date1,date2));
////
////int a=-3;
////        System.out.println(-a);
//        int diffValueEnd=-38;
//        System.out.println((-diffValueEnd-1));
//    }


    public static Date addAndSubtractDaysByGetTime(Date dateTime/*待处理的日期*/, int n/*加减天数*/) {

        //日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println(df.format(new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L)));
        //System.out.println(dd.format(new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L)));
        //注意这里一定要转换成Long类型，要不n超过25时会出现范围溢出，从而得不到想要的日期值
        return new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L);
    }

    public static void main(String[] arg) {
        Date now = new Date();

        addAndSubtractDaysByGetTime(now, -5);
        addAndSubtractDaysByGetTime(now, 5);
//        addAndSubtractDaysByCalendar(now,-5);
//        addAndSubtractDaysByCalendar(now,5);
    }


    /**
     * 得到字符串宽度
     *
     * @param str
     * @return
     */
//    public static int getStrWidth(String str) {
//        java.awt.FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(new java.awt.Font("Helvetica Neue", java.awt.Font.PLAIN, 12));
//        int w = fm.stringWidth(str);
//        return w;
//    }


    /**
     * 向上取舍
     * @param b1
     * @param b2
     * @param scale 小数位数
     * @return
     */
    public static BigDecimal devideResult(BigDecimal b1, BigDecimal b2, int scale) {
        BigDecimal divide = b1.divide(b2, scale, RoundingMode.HALF_UP);
        return divide;
    }
}
